<?php

namespace app\wp\controller;
use think\Request;
class Common extends \think\Controller{
    static public $treeList = array();
    public function _initialize() {
        parent::_initialize();
        //加载菜单
        $this->assign("menu" , $this->getMenu());
        //当前位置
        $this->assign("current" , $this->getCurrent());
        
        //获取当前登陆用户
        $this->assign("user" , null);
    }

    /**
     *  菜单级联（二级）
     */
    public function getMenu(){
        $menu = db("menu")->order("sort asc")->select();
        self::tree($menu);
    }
    
    /**
     *  获取当前位置
     */
    public function getCurrent(){
        $action = Request::instance()->action();
        $controller = Request::instance()->controller();
        $uri = "{$controller}/{$action}";
        $res =  db("menu")->field("name")->where(['url' => $uri])->find();
        return $res;
    }
    
    /**
     *  无限级分类
     */
    static public function tree($data,$pid=0,$level = 1){
        foreach($data as $v){
            if($v['pid']==$pid){
                $v['level']=$level;
                self::$treeList[]=$v;//将结果装到$treeList中
                self::tree($data,$v['id'],$level+1);
            }
        }
        return self::$treeList ;
    }
    
    
}
